package com.faiz.algorithm;

public class LessThanK {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        // 滑动窗口算法
        int n = nums.length;
        int left = 0, right = 0, res = 0, window = 1;
        while (right < n) {

            window *= nums[right];
            if (window >= k && left < right) {
                window /= nums[left];
                left++;
            }

            if (window < k) {
                int l = right - left;
                res += (l + l * l) / 2;
            }
            right++;
        }

        for (int i = 0; i < n; i++) {
            if (nums[i] < k) res++;
        }


        return res;
    }

    public static void main(String[] args) {
        LessThanK lessThanK = new LessThanK();
        int[] nums = new int[]{10, 5, 2, 6};
        int res = lessThanK.numSubarrayProductLessThanK(nums, 100);
        System.out.println(res);
    }
}
